<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Workerman\Worker;
use Workerman\Connection\TcpConnection;

class mqtt_sub extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mqtt_sub {action} {--daemonize}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'description';

    protected $client_id = 'php-server';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        global $argv;//定义全局变量
        $arg = $this->argument('action');
        $argv[1] = $arg;
        $argv[2] = $this->option('daemonize') ? '-d' : '';//该参数是以daemon（守护进程）方式启动

        $worker = new Worker('websocket://0.0.0.0:250');

        $worker->onMessage = function(TcpConnection $connection, $data)
        {
            //$connection->send("hello");
            /*if($data == 'REG_Syk000001'){
                $temp = $data;
                $handle = stream_socket_client("udp://119.23.152.198:7755", $errno, $errstr);
                if( !$handle ){
                    die("ERROR: {$errno} - {$errstr}\n");
                }
                fwrite($handle, $data);
                fclose($handle);
            }else{*/
                $arr = explode(',', $data);
                $czarr = $this->aa(intval($arr[0]),intval($arr[1]),intval($arr[2]),intval($arr[3]),intval($arr[4]),intval($arr[5]),intval($arr[6]),intval($arr[7]),intval($arr[8]),intval($arr[9]),intval($arr[10]),intval($arr[11]),intval($arr[12]),intval($arr[13]),intval($arr[14]),intval($arr[15]),intval($arr[16]),intval($arr[17]));
                //$temp = implode($czarr);
                $aba = '';
                for ($j = 0; $j < count($czarr); $j++) {
                    $aba .= chr($czarr[$j]);
                }
                $str = 'bus'.$aba;//$str = 'MSG_Syk000001bus'.$aba;
                $link = "udp://121.37.251.46:".$arr[18];
                $handle = stream_socket_client($link, $errno, $errstr);
                if( !$handle ){
                    echo("ERROR: {$errno} - {$errstr}\n");
                }
                fwrite($handle, $str);
                fclose($handle);
            //}
            var_dump($data);
        };

        // 运行worker
        Worker::runAll();
    }

    public function aa($c1,$c2,$c3,$c4,$c5,$c6,$c7,$c8,$c9,$c10,$c11,$c12,$c13,$c14,$c15,$c16,$c17,$c18)
    {
        $buf = [];
        $buf[0] = 0x0f;
        $buf[1] = $c1 & 0xff;
        $buf[2] = (($c1 >> 8)|($c2 << 3))& 0xff;
        $buf[3] = (($c2 >> 5)|($c3 << 6)) & 0xff;
        $buf[4] = ($c3 >> 2 )& 0xff;
        $buf[5] = (($c3 >> 10)|($c4 << 1))& 0xff;
        $buf[6] = (($c4 >> 7)|($c5 << 4))& 0xff;
        $buf[7] = (($c5 >> 4)|($c6 << 7))& 0xff;
        $buf[8] = ($c6 >> 1)& 0xff;
        $buf[9] = (($c6 >> 9)|($c7 << 2))& 0xff;
        $buf[10] = (($c7 >> 6)|($c8 << 5))& 0xff;
        $buf[11] = ($c8 >> 3)& 0xff;

        $buf[12] = $c9 & 0xff;
        $buf[13] = (($c9 >> 8)|($c10 << 3))& 0xff;
        $buf[14] = (($c10 >> 5)|($c11 << 6)) & 0xff;
        $buf[15] = ($c11 >> 2 )& 0xff;
        $buf[16] = (($c11 >> 10)|($c12 << 1))& 0xff;
        $buf[17] = (($c12 >> 7)|($c13 << 4))& 0xff;
        $buf[18] = (($c13 >> 4)|($c14 << 7))& 0xff;
        $buf[19] = ($c14 >> 1)& 0xff;
        $buf[20] = (($c14 >> 9)|($c15 << 2))& 0xff;
        $buf[21] = (($c15 >> 6)|($c16 << 5))& 0xff;
        $buf[22] = ($c16 >> 3)& 0xff;

        $buf[23] = ($c17 << 7 )|( $c18 << 6);
        $buf[24] = 0x00;
        return $buf;
    }
}
